package com.yrmao.chiefenergyofficer.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.List;

/**
 * Created by yemao on 16/11/16.
 */

public class MPagerAdapter extends android.support.v4.view.PagerAdapter {
    private List<Integer> mList;
    private Context mContext;
    private ImageView itemImagView;
    private RelativeLayout layout;

    public MPagerAdapter(Context context, List<Integer> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 类似于BaseAdapger的getView方法 用了将数据设置给view 由于它最多就3个界面，不需要viewHolder
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        View view = View.inflate(mContext, R.layout.carouse_item, null);
//        ImageView imageView = (ImageView) view.findViewById(R.id.image);
//
//        imageView.setImageResource(mList.get(position % mList.size()));
        initView();
        View view = setView(mList.get(position % mList.size()));

        container.addView(view);// 一定不能少，将view加入到viewPager中

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    private ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    private void initView() {
        layout = new RelativeLayout(mContext);
        itemImagView = new ImageView(mContext);
        layout.setLayoutParams(lp);
        itemImagView.setLayoutParams(lp);
    }

    private View setView(Integer imgId) {
        itemImagView.setBackgroundResource(imgId);
        layout.removeAllViews();
        layout.addView(itemImagView);
        return layout;
    }
}
